/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uk.ac.cam.juliet.zedcat.client;

import java.awt.Rectangle;
import java.util.List;

/**
 *
 * @author Chris
 */
// Event factory takes a region and generates an Event for it.
public class EventFactory {

    public static Event ProcessRegion(Region r, Configuration conf, Pipeline p) {
        List<Hotspot> hotspots = conf.getHotspots();

        Rectangle rpos = r.getLocation();
        Event e = new Event();
        e.setRegion(r);

        // Enumerate hotspot distances for this region.
        for (Hotspot h: hotspots)
            e.addHotspot(h, RectDistance(h, rpos));

        return e;
    }

    // A "natural" distance between two bounding boxes.
    // Approximation to elliptical bounding.
    private static double RectDistance(Rectangle r1, Rectangle r2) {
        return Math.min(RectCentreToEdge(r1, r2), RectCentreToEdge(r2, r1));
    }

    // Get the distance from one Rectangles edge to the centre of the other.
    private static double RectCentreToEdge(Rectangle r1, Rectangle r2) {

        // r1 center
        double r1_x = r1.getCenterX(), r1_y = r1.getCenterY(), xd, yd;

        // x distance
        if (r1_x >= r2.getMinX()) {
            if (r1_x <= r2.getMaxX()) {
                xd = 0;
            } else {
                xd = r1_x - r2.getMaxX();
            }
        } else {
            return xd = r2.getMinX() - r1_x;
        }

        // y distance
        if (r1_y >= r2.getMinY()) {
            if (r1_y <= r2.getMaxY()) {
                yd = 0;
            } else {
                yd = r1_y - r2.getMaxY();
            }
        } else {
            yd = r2.getMinY() - r1_y;
        }

        return Math.sqrt(Math.pow(xd, 2) + Math.pow(yd, 2));
    }

}
